package com.hxx.service.impl;

import com.hxx.domain.TUser;
import com.hxx.service.UserService;
import com.hxx.uitls.DbPoolConnection;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class UserServiceImpl implements UserService {

    private Connection connection = DbPoolConnection.getInstance().getConnection();

   @Override
    public void addUser(TUser tUser) {
        try {
            String sql = "insert into t_user(username,sex,age) values(?,?,?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            Statement statement = null;

            // 为占位符设置参数
            preparedStatement.setString(1,tUser.getUsername());
            preparedStatement.setString(2,tUser.getSex());
            preparedStatement.setInt(3,tUser.getAge());
            //preparedStatement.executeUpdate();
            preparedStatement.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public TUser getUser(Integer id) {
        TUser tUser = new TUser();
        try {
            String sql = "select * from t_user where id = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            // 为占位符设置参数
            preparedStatement.setInt(1,id);
            ResultSet resultSet = preparedStatement.executeQuery();
            //必须让游标指向第一行数据
            if (resultSet.next()) {
                tUser.setId(resultSet.getInt("id"));
                tUser.setUsername(resultSet.getString("username"));
                tUser.setSex(resultSet.getString("sex"));
                tUser.setAge(resultSet.getInt("age"));
            }
        }catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return tUser;
    }

    @Override
    public List<TUser> getAllUser() {
       List<TUser> list = null;
        try {
            list = new ArrayList<>();
            String sql = "select * from t_user";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            // 执行查询操作，获取结果集
            ResultSet resultSet = preparedStatement.executeQuery();
            //必须让游标指向第一行数据
            while (resultSet.next()) {
                TUser tUser = new TUser();
                tUser.setId(resultSet.getInt("id"));
                tUser.setUsername(resultSet.getString("username"));
                tUser.setSex(resultSet.getString("sex"));
                tUser.setAge(resultSet.getInt("age"));
                list.add(tUser);
            }
        }catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    @Override
    public void updateUser(TUser tUser) {
        try {
            String sql = "update t_user set username=?,sex=?,age=? where id=?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            // 为占位符设置参数
            preparedStatement.setString(1,tUser.getUsername());
            preparedStatement.setString(2,tUser.getSex());
            preparedStatement.setInt(3,tUser.getAge());
            preparedStatement.setInt(4,tUser.getId());
            // 执行更新操作
            preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public void deleteUser(Integer id) {
        try {
            String sql = "delete from t_user where id=?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            // 为占位符设置参数
            preparedStatement.setInt(1,id);
            // 执行删除操作
            preparedStatement.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}



























